﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using UFrame;
using System.Collections.Specialized;

#if UNITY_STANDALONE_OSX
using WindowsTool = OsxWindowsTool;
#endif

public class FileItem : MonoBehaviour
{
    [SerializeField]
    protected Text m_size;
    [SerializeField]
    protected Text m_name;
    [SerializeField]
    protected RawImage m_image;
    [SerializeField]
    protected Button m_copy;
    [SerializeField]
    protected Button m_open;
    [SerializeField]
    protected Button m_locate;
    [SerializeField]
    protected Button m_asIconBtn;
    [SerializeField]
    protected Button m_executeBtn;

    public System.Action<Texture2D> onAsIconBtnClick { get; set; }
    public string filePath { get; set; }
    protected static string[] keyFrames = new string[] { "B", "KB", "MB", "GB" };
    protected AspectRatioFitter m_aspectRatio;
    private void Awake()
    {
        m_copy.onClick.AddListener(OnCopyCurrentItem);
        m_open.onClick.AddListener(OnOpenFileClick);
        m_asIconBtn.onClick.AddListener(OnSetAsIcon);
        m_locate.onClick.AddListener(OnLocateTo);
        m_aspectRatio = m_image.GetComponent<AspectRatioFitter>();
        m_executeBtn.onClick.AddListener(OnExecuteClick);
    }

    private void OnLocateTo()
    {
        //WindowsTool.OpenFolderAndSelectFile(filePath);
        var ext = System.IO.Path.GetExtension(filePath);
        var fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
        var exportPath = WindowsTool.SaveFilePath("导出到", fileName, ext.Replace(".",""));
        if (!string.IsNullOrEmpty(exportPath))
        {
            WindowsTool.CopyFile(filePath, exportPath);
        }
    }

    private void OnSetAsIcon()
    {
        if (onAsIconBtnClick != null && m_image.texture)
            onAsIconBtnClick.Invoke(m_image.texture as Texture2D);
    }

    private void OnOpenFileClick()
    {
        WindowsTool.OpenFile(filePath);
    }

    private void OnExecuteClick()
    {
        //WindowsTool.Execute(filePath);
    }

    private void OnCopyCurrentItem()
    {
        TextEditor te = new TextEditor();
        te.text = filePath;
        te.OnFocus();
        te.Copy();
    }

    public void ShowFile(string filePath)
    {
        this.filePath = filePath;
        m_name.text = System.IO.Path.GetFileName(filePath);
        m_size.text = GetFileSizeText(filePath);
        var fileExt = System.IO.Path.GetExtension(filePath);
        bool canAsIcon = false;
        bool executeAble = false;
        switch (fileExt)
        {
            case ".mp3":
            case ".wav":
            case ".ogg":
                LoadInnerIcon("audio");
                break;
            case ".unitypackage":
                LoadInnerIcon("unitypackage");
                break;
            case ".jpeg":
            case ".jpg":
            case ".png":
            case ".tga":
                LoadTexture(filePath);
                canAsIcon = true;
                break;
            case ".bmp":
                //LoadBitMap(filePath);
                LoadTexture(filePath);
                canAsIcon = true;
                break;
            case ".py":
                LoadInnerIcon("python");
                canAsIcon = true;
                executeAble = true;
                break;
            default:
                LoadInnerIcon("unknown");
                break;
        }
        m_asIconBtn.gameObject.SetActive(canAsIcon && m_image.texture != null);
        m_executeBtn.gameObject.SetActive(executeAble);
    }

    //private void LoadBitMap(string filePath)
    //{
    //    var texture = ImageToTexture.LoadBitmapTexture(filePath);
    //    if (texture != null)
    //    {
    //        SetTexture(texture);
    //    }
    //    else
    //    {
    //        LoadInnerIcon("unknown");
    //    }
    //}
    private void LoadTexture(string filePath)
    {
        var bytes = System.IO.File.ReadAllBytes(filePath);
        var texture = new Texture2D(0, 0);
        if (texture.LoadImage(bytes))
        {
            SetTexture(texture);
        }
        else
        {
            LoadInnerIcon("unknown");
        }

    }

    protected string GetFileSizeText(string filePath)
    {
        if (System.IO.File.Exists(filePath))
        {
            FileInfo fileInfo = new FileInfo(filePath);
            var size = fileInfo.Length;
            for (int i = 0; i < keyFrames.Length; i++)
            {
                var fliter = Mathf.Pow(1024, i);
                if (size < fliter * 1024)
                {
                    return (size / fliter).ToString("0.00") + keyFrames[i];
                }
            }
            return size + "(B)";
        }
        return "Unknown";
    }

    protected void LoadInnerIcon(string iconName)
    {
        //Debug.LogError("LoadInnerIcon:" + iconName);

        var texture = SourceManager.Instance.LoadSource<Texture>("Icons/" + iconName, UFrame.Source.SourceLoadOption.None);
        SetTexture(texture);
    }
    protected Vector3[] coners = new Vector3[4];
    protected void SetTexture(Texture texture)
    {
        m_image.texture = texture;
        if (texture != null && m_aspectRatio)
        {
            var retio = (float)texture.width / texture.height;
            m_aspectRatio.aspectRatio = retio;
        }
    }
}
